package com.castle.fortress.admin.cms.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.castle.fortress.admin.cms.dto.CmsLinkCategoryDto;
import com.castle.fortress.admin.cms.entity.CmsLinkCategoryEntity;
import com.castle.fortress.admin.cms.mapper.CmsLinkCategoryMapper;
import com.castle.fortress.admin.cms.service.CmsLinkCategoryService;
import com.castle.fortress.common.entity.RespBody;
import com.castle.fortress.common.respcode.BizErrorCode;
import com.castle.fortress.common.utils.ConvertUtil;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * cms友链分类管理表 服务实现类
 *
 * @author castle
 * @since 2022-02-24
 */
@Service
public class CmsLinkCategoryServiceImpl extends ServiceImpl<CmsLinkCategoryMapper, CmsLinkCategoryEntity> implements CmsLinkCategoryService {
    /**
    * 获取查询条件
    * @param cmsLinkCategoryDto
    * @return
    */
    public QueryWrapper<CmsLinkCategoryEntity> getWrapper(CmsLinkCategoryDto cmsLinkCategoryDto){
        QueryWrapper<CmsLinkCategoryEntity> wrapper= new QueryWrapper();
        if(cmsLinkCategoryDto != null){
            CmsLinkCategoryEntity cmsLinkCategoryEntity = ConvertUtil.transformObj(cmsLinkCategoryDto,CmsLinkCategoryEntity.class);
            wrapper.like(StrUtil.isNotEmpty(cmsLinkCategoryEntity.getName()),"name",cmsLinkCategoryEntity.getName());
            wrapper.like(StrUtil.isNotEmpty(cmsLinkCategoryEntity.getCode()),"code",cmsLinkCategoryEntity.getCode());
        }
        wrapper.orderByAsc("sort");
        return wrapper;
    }


    @Override
    public IPage<CmsLinkCategoryDto> pageCmsLinkCategory(Page<CmsLinkCategoryDto> page, CmsLinkCategoryDto cmsLinkCategoryDto) {
        QueryWrapper<CmsLinkCategoryEntity> wrapper = getWrapper(cmsLinkCategoryDto);
        Page<CmsLinkCategoryEntity> pageEntity = new Page(page.getCurrent(), page.getSize());
        Page<CmsLinkCategoryEntity> cmsLinkCategoryPage=baseMapper.selectPage(pageEntity,wrapper);
        Page<CmsLinkCategoryDto> pageDto = new Page(cmsLinkCategoryPage.getCurrent(), cmsLinkCategoryPage.getSize(),cmsLinkCategoryPage.getTotal());
        pageDto.setRecords(ConvertUtil.transformObjList(cmsLinkCategoryPage.getRecords(),CmsLinkCategoryDto.class));
        return pageDto;
    }


    @Override
    public List<CmsLinkCategoryDto> listCmsLinkCategory(CmsLinkCategoryDto cmsLinkCategoryDto){
        QueryWrapper<CmsLinkCategoryEntity> wrapper = getWrapper(cmsLinkCategoryDto);
        List<CmsLinkCategoryEntity> list = baseMapper.selectList(wrapper);
        return ConvertUtil.transformObjList(list,CmsLinkCategoryDto.class);
    }

    @Override
    public List<CmsLinkCategoryDto> cmsTagListCmsLinkGroup(Map<String, Object> map) {
        return ConvertUtil.transformObjList(baseMapper.cmsTagListCmsLinkGroup(map), CmsLinkCategoryDto.class);
    }

    @Override
    public RespBody checkColumnRepeat(CmsLinkCategoryDto cmsLinkCategoryDto) {
        //校验code
        QueryWrapper<CmsLinkCategoryEntity> queryWrapper=new QueryWrapper();
        queryWrapper.eq("code",cmsLinkCategoryDto.getCode());
        List<CmsLinkCategoryEntity> list = baseMapper.selectList(queryWrapper);
        if(list != null && !list.isEmpty()){
            //新增
            if(cmsLinkCategoryDto.getId() == null){
                return RespBody.fail(BizErrorCode.CMS_LINKGROUP_CODE_EXIST_ERROR);
                //修改
            }else{
                for(CmsLinkCategoryEntity entity:list){
                    if(!entity.getId().equals(cmsLinkCategoryDto.getId())){
                        return RespBody.fail(BizErrorCode.CMS_LINKGROUP_CODE_EXIST_ERROR);
                    }
                }
            }
        }
        return RespBody.data("校验通过");
    }
}

